package com.smedi.ismedi.constructionproject.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smedi.ismedi.constructionproject.api.dto.request.BatchExistingPersonRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentEditRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectAssignmentForMobileResp;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectAssignmentResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.constructionproject.core.application.command.ProjectAssignmentCommand;
import com.smedi.ismedi.constructionproject.core.application.command.ProjectAssignmentEditCommand;
import com.smedi.ismedi.constructionproject.core.application.command.ProjectAssignmentListQueryCommand;
import com.smedi.ismedi.constructionproject.core.application.query.ProjectAssignmentQueryService;
import com.smedi.ismedi.constructionproject.core.application.query.vo.ProjectAssignmentVo;
import com.smedi.ismedi.constructionproject.core.application.service.ProjectAssignmentApplicationService;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.ProjectAssignment;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.ProjectAssignmentForMobile;
import com.smedi.ismedi.constructionproject.core.port.adapter.restful.convert.AdapterConverter;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: xulinjun
 * @Date: 2021-6-21 10:16
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ProjectAssignmentController implements RemoteProjectAssignmentFeignApi {
    private final DozerUtil dozerUtil;
    private final ProjectAssignmentApplicationService projectAssignmentApplicationService;
    private final ProjectAssignmentQueryService projectAssignmentQueryService;
    private final AdapterConverter adapterConverter;

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<ProjectAssignmentResponse> postSave(ProjectAssignmentRequest projectAssignmentRequest) {
        ProjectAssignmentCommand projectAssignmentCommand = dozerUtil.convertor(projectAssignmentRequest, ProjectAssignmentCommand.class);
        ProjectAssignment projectAssignment = projectAssignmentApplicationService.saveProjectAssignment(projectAssignmentCommand);
        return new ResponseData<ProjectAssignmentResponse>().success(dozerUtil.convertor(projectAssignment, ProjectAssignmentResponse.class));
    }

    @Override
    public ResponseData<Boolean> editProjectAssignment(ProjectAssignmentEditRequest projectAssignmentEditRequest) {
        ProjectAssignmentEditCommand projectAssignmentEditCommand = adapterConverter.projectAssignmentEditRequestToProjectAssignmentEditCommand(projectAssignmentEditRequest);
        projectAssignmentApplicationService.editProjectAssignment(projectAssignmentEditCommand);
        return new ResponseData<>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postBatchAddExistingPerson(BatchExistingPersonRequest batchExistingPersonRequest) {
        projectAssignmentApplicationService.postBatchAddExistingPerson(batchExistingPersonRequest.getPersonIds(), batchExistingPersonRequest.getProjectId(), batchExistingPersonRequest.getProjectOrganizationId());
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteProjectAssignment(Long projectAssignmentId) {
        projectAssignmentApplicationService.deleteProjectAssignment(projectAssignmentId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<PaginationData<ProjectAssignmentResponse>> getQueryProjectAssignmentPersonInfoPagination(Long offset, Long pageSize, Long projectId, Long projectOrganizationId, String lastName, String userName, String employeeNum, String mobile, boolean containProjectOrganizationChildren) {
        IPage pagination;

        if (containProjectOrganizationChildren) {
            pagination = projectAssignmentQueryService.getProjectAssignmentPaginationContainsChildren(offset, pageSize, projectId, projectOrganizationId, lastName, userName, employeeNum, mobile);
        } else {
            pagination = projectAssignmentQueryService.getProjectAssignmentPagination(offset, pageSize, projectId, projectOrganizationId, lastName, userName, employeeNum, mobile);
        }

        pagination.setRecords(dozerUtil.convertor(pagination.getRecords(), ProjectAssignmentResponse.class));

        PaginationData<ProjectAssignmentResponse> paginationResponse = dozerUtil.convertor(pagination, PaginationData.class);
        return new ResponseData<PaginationData>().success(paginationResponse);
    }

    @Override
    public ResponseData<ProjectAssignmentForMobileResp> getQueryProjectAssignmentPersonInfo(Long projectId, Long projectOrganizationId, String lastName, String userName, String employeeNum, String mobile, boolean containProjectOrganizationChildren) {
        ProjectAssignmentForMobile projectAssignmentForMobile;
        if (containProjectOrganizationChildren) {
            projectAssignmentForMobile = projectAssignmentQueryService.getProjectAssignmentContainsChildren(projectId, projectOrganizationId, lastName, userName, employeeNum, mobile);
        } else {
            projectAssignmentForMobile = projectAssignmentQueryService.getProjectAssignment(projectId, projectOrganizationId, lastName, userName, employeeNum, mobile);
        }


        return new ResponseData<PaginationData>().success(dozerUtil.convertor(projectAssignmentForMobile, ProjectAssignmentForMobileResp.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<Long>> getQueryPersonIdsByProjectId(Long projectId) {
        List<Long> personIds = projectAssignmentQueryService.getPersonIdsByProjectIdNoToken(projectId);
        return new ResponseData<List<Long>>().success(personIds);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Map<Long, String>> getQueryProjectNamesByPersonIds(List<Long> personIds) {
        Map<Long, String> maps = projectAssignmentQueryService.getProjectNamesByPersonIds(personIds);
        return new ResponseData<Map<Long, String>>().success(maps);
    }

    @Override
    public ResponseData<List<ProjectAssignmentResponse>> getQueryProjectAssignmentsByPersonIds(List<Long> personIds) {
        List<ProjectAssignment> projectAssignments = projectAssignmentQueryService.getByPersonIds(personIds);
        return new ResponseData<>().success(dozerUtil.convertor(projectAssignments, ProjectAssignmentResponse.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<ProjectAssignmentResponse>> getQueryProjectAssignmentsByPersonId(Long personId) {
        List<ProjectAssignmentVo> list = projectAssignmentQueryService.getProjectAssignmentsByPersonId(personId);
        List<ProjectAssignmentResponse> responses = dozerUtil.convertor(list, ProjectAssignmentResponse.class);
        return new ResponseData<List<ProjectAssignmentResponse>>().success(responses);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteProjectAssignmentsByPersonId(Long personId) {
        projectAssignmentApplicationService.deleteProjectAssignmentsByPersonId(personId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteProjectAssignmentsByPersonIdAndProjectId(Long personId, Long projectId) {
        projectAssignmentApplicationService.deleteProjectAssignmentsByPersonIdAndProjectId(personId, projectId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> getValidateProjectOrganizationHasAssignment(Long projectOrganizationId) {
        Boolean result = projectAssignmentApplicationService.validateProjectOrganizationHasAssignment(projectOrganizationId);
        return new ResponseData<Boolean>().success(result);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<List<Long>> getQueryPersonSimpleByCurrentProjectOrganizationId(Long projectOrganizationId) {
        List<Long> personIds = projectAssignmentApplicationService.queryPersonIdsByCurrentProjectOrganizationId(projectOrganizationId);
        return new ResponseData<List<Long>>().success(personIds);
    }

    @Override
    public ResponseData<List<ProjectAssignmentResponse>> getQueryProjectAssignmentsByPersonIdNoToken(Long personId) {
        List<ProjectAssignment> list = projectAssignmentQueryService.getProjectAssignmentsByPersonIdNoToken(personId);
        List<ProjectAssignmentResponse> responses = dozerUtil.convertor(list, ProjectAssignmentResponse.class);
        return new ResponseData<List<ProjectAssignmentResponse>>().success(responses);
    }

    @Override
    public ResponseData<List<ProjectAssignmentResp>> getProjectAssignmentList(ProjectAssignmentListQueryRequest projectAssignmentListQueryRequest) {
        ProjectAssignmentListQueryCommand projectAssignmentListQueryCommand = dozerUtil.convertor(projectAssignmentListQueryRequest, ProjectAssignmentListQueryCommand.class);
        List<ProjectAssignment> projectAssignments = projectAssignmentQueryService.getProjectAssignmentList(projectAssignmentListQueryCommand);
        return new ResponseData<List<ProjectAssignmentResponse>>().success(dozerUtil.convertor(projectAssignments, ProjectAssignmentResp.class));
    }

    @Override
    public ResponseData<List<ProjectAssignmentResp>> getProjectAssignmentListWithoutToken(ProjectAssignmentListQueryRequest projectAssignmentListQueryRequest) {
        ProjectAssignmentListQueryCommand projectAssignmentListQueryCommand = dozerUtil.convertor(projectAssignmentListQueryRequest, ProjectAssignmentListQueryCommand.class);
        List<ProjectAssignment> projectAssignments = projectAssignmentQueryService.getProjectAssignmentListWithoutToken(projectAssignmentListQueryCommand);
        return new ResponseData<List<ProjectAssignmentResponse>>().success(dozerUtil.convertor(projectAssignments, ProjectAssignmentResp.class));
    }
}
